package com.hzdesk.media.koreabatchmatvector.dialog;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.hzdesk.media.koreabatchmatvector.R;
import com.hzdesk.media.koreabatchmatvector.inter.RightViewClick;


/**
 * Created by Administrator on 2018/4/14.
 */

public class RightViewPopWindow extends PopupWindow implements View.OnClickListener{

    private View contantView;
    private TextView text_back,text_clear,text_cancel,text_undo,text_redo;
    private RightViewClick mRightViewClick;
    public void setOnRightViewClickListener(RightViewClick rig){
        this.mRightViewClick=rig;
    }

    public RightViewPopWindow(Activity context) {
        super(context);
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        contantView=inflater.inflate(R.layout.right_view,null);
        text_back= (TextView) contantView.findViewById(R.id.right_view_back);
        text_cancel= (TextView) contantView.findViewById(R.id.right_view_cancel);
        text_clear= (TextView) contantView.findViewById(R.id.right_view_clear);
        text_undo= (TextView) contantView.findViewById(R.id.right_view_undo);
        text_redo= (TextView) contantView.findViewById(R.id.right_view_redo);
        text_clear.setOnClickListener(this);
        text_cancel.setOnClickListener(this);
        text_back.setOnClickListener(this);
        text_undo.setOnClickListener(this);
        text_redo.setOnClickListener(this);
        int h = context.getWindowManager().getDefaultDisplay().getHeight();
        int w = context.getWindowManager().getDefaultDisplay().getWidth();
        // 设置SelectPicPopupWindow的View
        this.setContentView(contantView);
        // 设置SelectPicPopupWindow弹出窗体的宽
        this.setWidth(h/5);
        // 设置SelectPicPopupWindow弹出窗体的高
        this.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        // 设置SelectPicPopupWindow弹出窗体可点击
        this.setFocusable(true);
        this.setOutsideTouchable(true);
        // 刷新状态
        this.update();
        // 实例化一个ColorDrawable颜色为半透明
        ColorDrawable dw = new ColorDrawable(0000000000);
        // 点back键和其他地方使其消失,设置了这个才能触发OnDismisslistener ，设置其他控件变化等操作
        this.setBackgroundDrawable(dw);
        // mPopupWindow.setAnimationStyle(android.R.style.Animation_Dialog);
        // 设置SelectPicPopupWindow弹出窗体动画效果
        //this.setAnimationStyle(R.style.AnimationPreview);
    }

    public void showPopupWindow(int x, int y) {
        if (!this.isShowing()) {
            this.showAtLocation(contantView, Gravity.NO_GRAVITY,x,y);
            //this.showAsDropDown(contantView, Gravity.NO_GRAVITY,x,y);
        } else {
            this.dismiss();
        }

    }

    public void dissmissWindow(){
        if (this.isShowing()){
            this.dismiss();
        }
    }




    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.right_view_back:
                mRightViewClick.clickback(1);
                this.dismiss();
                break;
            case R.id.right_view_cancel:
                mRightViewClick.clickback(2);
                this.dismiss();
                break;
            case R.id.right_view_clear:
                mRightViewClick.clickback(3);
                this.dismiss();
                break;
            case R.id.right_view_undo:
                mRightViewClick.clickback(4);
                this.dismiss();
                break;
            case R.id.right_view_redo:
                mRightViewClick.clickback(5);
                this.dismiss();
                break;

        }

    }


}
